﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using cz.muni.fi.pb138.Sessions;


namespace cz.muni.fi.pb138
{
    public partial class LoginScreen : Form
    {
        public LoginScreen()
        {
            InitializeComponent();
        }

		/// <summary>
		/// Quit button pressed. The application is closed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void cancelButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

		/// <summary>
		/// login button pressed. Attempt of a login is performed under given login and password
		/// inserted in <c>loginBox</c> and <c>passBox</c>. If the connection to the database is
		/// successful and the login and password corresponds to an existing user in the DB, than
		/// the MainForm.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void loginButton_Click(object sender, EventArgs e)
        {
            if (this.loginBox.Text == "users" || this.loginBox.Text == "Users")
            {
                erroLabel.Text = "Wrong username or password!";
                return;
            }

            if (String.IsNullOrEmpty(this.loginBox.Text) || String.IsNullOrEmpty(this.passBox.Text))
            {
                erroLabel.Text = "Please enter all login details!";
                return;
            }

            if (!AdminSession.HasServerConnection())
            {
                erroLabel.Text = "Failed to connect server!";
                return;
            }
            erroLabel.Text = "";
            
            
            using (QuerySession s = new QuerySession())
            {
                MediaManager.Login = this.loginBox.Text;
                if (!s.AuthenticateUser(this.loginBox.Text, this.passBox.Text))
                {
                    erroLabel.Text = "Wrong username or password!";
                    return;
                }
            }

            loadingLabel.Visible = true;
            MainForm main;
            try
            {
                main = new MainForm();
                main.Show();
                this.Visible = false;

            }
            catch (Exception ex)
            {
                loadingLabel.Visible = false;
                erroLabel.Text = "Error occuured! Cannot start appliciation!";
                MessageBox.Show(ex.Message);
            }
        }

		/// <summary>
		/// Register button hit. Opens a new <c>RegisterForm</c> window to allow a registration
		/// of a new user
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void RegistrationWasClicked(object sender, EventArgs e)
        {         
            RegisterForm reg = new RegisterForm();
            if (reg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Registration was successful !", "Successful registration", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}
